package cn.net.sichen.ekinvoker.controller;

import com.netflix.appinfo.InstanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Configuration
public class InvokerController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/router")
    public String router(){
        List<String> services = discoveryClient.getServices();
        for (String id : services) {
            List<ServiceInstance> instances = discoveryClient.getInstances(id);
            for (ServiceInstance instance : instances) {
                EurekaDiscoveryClient.EurekaServiceInstance eurekaServiceInstance = (EurekaDiscoveryClient.EurekaServiceInstance)instance;
                InstanceInfo instanceInfo = eurekaServiceInstance.getInstanceInfo();
                System.out.println(instanceInfo.getAppName() + "---" +
                instanceInfo.getInstanceId() + "---" +
                instanceInfo.getStatus());
            }
        }
        
        return "";
    }
}
